// components/authorization/index.js
Component({
  data: {
    isLogin: false
  },
  lifetimes: {
    // 组件在节点树中挂载完毕
    attached() {
      console.log('~~~~~~')
      // const app = getApp()
      // const token = app.token
      // const isLogin = !!token
      const isLogin = !!getApp().token
      this.setData({
        isLogin
      })
      // 没有登录拦截到登录页
      if (!isLogin) {
        const currentPages = getCurrentPages()
        const currentPage = currentPages[currentPages.length - 1]
        // 没有登录覆盖 onLoad，onLoad 里面如果有请求也会完全覆盖
        currentPage.onLoad = () => {}
        currentPage.onShow = () => {}
        const isTabBarPage = wx.utils.isTabBarPage(currentPage.route)
        const routeType = isTabBarPage ? 'switchTab' : 'redirectTo'
        wx.redirectTo({
          url: `/pages/login/index?redirectUrl=/${currentPage.route}&routeType=${routeType}`,
        })
      }
    }
  }
})